﻿using Quartz;
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Mvc;
using WebApp.Models;
using Services;
using Models;
using System.Collections.Generic;

namespace WebApp.App_Start
{
    public class IndexJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                string _movies = Read(Val.CMSPath.TrimEnd('\\') + @"\Admins\Templates\index.html");
                string _renderBody = GetBody();

                string html = _movies.Replace("{$body$}", _renderBody);
                Write(html, Val.CMSPath.TrimEnd('\\') + @"\index.html");
            }
            catch (Exception ex)
            {
                string meesage = ex.Message;
                if (ex.InnerException != null)
                    meesage += ex.InnerException;
                Write(ex.Message, Val.CMSPath.TrimEnd('\\') + @"\log.txt");
            }
        }

        public string GetBody()
        {
            ContentsServices contentSvrs = new ContentsServices();
            CategoryServices categorySvrs = new CategoryServices();
            IndexViewModels retResult = new IndexViewModels();

            retResult.LastContents = contentSvrs.GetTopList(1, 15);
            retResult.CoverContents = contentSvrs.GetCover(1, 5);
            List<Category> _categoryList = categorySvrs.GetList(t => t.CategoryName != "");
            List<IndexViewModel> _indexModels = new List<IndexViewModel>();
            foreach (var _cate in _categoryList)
            {
                IndexViewModel model = new IndexViewModel();
                model.Category = _cate;
                model.ContentList = contentSvrs.GetListByCategory(_cate.CategoryUrl, 1, 10);

                _indexModels.Add(model);
            }
            retResult.IndexModels = _indexModels;
            StringBuilder html = new StringBuilder();
            html.Append(@"
<link href='/Content/auto1.css' rel='stylesheet' />
<div class='col'>
    <div id='newest' class='fr'>
        <h2>最新文章<span style='float:right'><a href='/movies/' target='_blank'>更多&gt;&gt;</a></span></h2>
        <ul>");
            foreach (var item in retResult.LastContents)
            {
                html.Append(@"<li>
                    <a style='float:left' href='/view/" + item.Seed / 1000 + "/" + item.Seed.ToString() + @".html' rel='bookmark'
                       title='《" + item.Name + @"》高清迅雷下载' target='_blank'>《" + item.Name + @"》高清迅雷下载</a>
                    <div style='float:right;'><span style='color:#CC0000'>[" + item.State + @"]</span><span style='color:#666666'>" + item.UpdateTime.ToString("MM-dd") + @"</span></div>
                </li>");
            }

            html.Append(@"</ul>
    </div>
    <div id='slideBox'>
        <ul id='show_pic' style='left: -774px;'>");

            foreach (var cover in retResult.CoverContents)
            {
                html.Append(@"<li><a href='/view/"+ cover.Seed/1000+"/" + cover.Seed + ".html' target='_blank'><img alt='" + cover.Name + "' title='' height='360' width='258' src='" + cover.ImgUrl + "'></a></li>");
            }
            html.Append(@"</ul>
        <div id='slideText'>
        </div>
        <ul id='iconBall'>");

            foreach (var cover in retResult.CoverContents)
            {
                html.Append(@"<li class=''>" + (retResult.CoverContents.IndexOf(cover) + 1) + "</li>");
            }
            html.Append(@"</ul>
        <ul id='textBall'>");
            foreach (var cover in retResult.CoverContents)
            {
                html.Append(@"<li class='active'><a href='javascript:void(0)'>" + cover.Name + "</a></li>");
            }
            html.Append(@"</ul>
    </div>
</div>
<script src = '/Scripts/zzsc.js' ></script>");
            foreach (var indexModel in retResult.IndexModels)
            {
                html.Append(@"<div class='col"+(2-(retResult.IndexModels.IndexOf(indexModel)+1)%2)+@"'>
        <div class='catlist'>
            <h3>
                <a href = '/category/"+indexModel.Category.CategoryUrl+ @"/' target='_blank'>" + indexModel.Category.CategoryName + @"</a>
                <span>
                    <a href = '/category/" + indexModel.Category.CategoryUrl + @"/' target='_blank'>更多&gt;&gt;</a>
                </span>
            </h3>
            <ul>");
                foreach (var content in indexModel.ContentList)
                {
                    html.Append(@"<li >
                        <a style = 'float:left' href = '/view/"+content.Seed/1000+"/"+content.Seed+@".html' target = '_blank' >《"+content.Name+ @"》高清迅雷下载 </a>
                                <div style = 'float:right' ><span style = 'color:#CC0000' >[" + content.State + @"] </span><span style = 'color:#666666'>" + content.UpdateTime.ToString("MM-dd") + @"</span></div></li>");
                }
                html.Append("</ul></div></div>");
            }

            return html.ToString();
        }
public static void Write(string content, string path)
{
    //if not exists is create
    FileInfo file = new FileInfo(path);
    if (!file.Directory.Exists)
        file.Directory.Create();

    FileStream fs = new FileStream(path, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);
    //开始写入
    sw.Write(content);
    //清空缓冲区
    sw.Flush();
    //关闭流
    sw.Close();
    fs.Close();
}
public static string Read(string path)
{
    string retVal = string.Empty;
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    StreamReader sr = new StreamReader(fs, Encoding.UTF8);
    retVal = sr.ReadToEnd();
    fs.Close();
    sr.Close();

    return retVal;
}
    }

    public static class Val
{
    public static string CMSPath { get; set; }
}
}